﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Skybot.Docs;
public partial class Near : System.Web.UI.Page
{
    /// <summary>
    /// url
    /// </summary>
    public string url = string.Empty;
    /// <summary>
    /// 显示标识
    /// </summary>
    public bool tag = false;

    /// <summary>
    /// 编码
    /// </summary>
    public string encode = "UTF-8";
    protected void Page_Load(object sender, EventArgs e)
    {

        url = Geturl();
        bool.TryParse(GetTag(), out tag);
        encode = GetEncode();
        encode = string.IsNullOrEmpty(encode) ? "UTF-8" : encode;
        if (!string.IsNullOrEmpty(url))
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            wc.Encoding = System.Text.Encoding.GetEncoding(encode);
            string str = wc.DownloadString(url);
            string stra = str.ReplaceByNearSynonym(tag);
            Response.Write(stra);
            wc.Dispose();
        }
    }

    /// <summary>
    /// 获得需要处理的URL
    /// </summary>
    /// <returns>返回数据如果没有则返回1</returns>
    public string Geturl()
    {
        string keyword = Request["u"];
        //url重写类型
        string urlRewriteKeyword = RouteData.Values["u"] as string;
        string temp = keyword == null && urlRewriteKeyword != null ? urlRewriteKeyword : null;
        temp = string.IsNullOrEmpty(keyword) ? temp : keyword;
        temp = temp == null ? null : temp;
        //替换URL 因为URL在重写的时候会被翻译
        return temp;
    }

    /// <summary>
    /// 获得目标URL的编码类型
    /// </summary>
    /// <returns>返回数据如果没有则返回1</returns>
    public string GetEncode()
    {
        string keyword = Request["encode"];
        //url重写类型
        string urlRewriteKeyword = RouteData.Values["encode"] as string;
        string temp = keyword == null && urlRewriteKeyword != null ? urlRewriteKeyword : null;
        temp = string.IsNullOrEmpty(keyword) ? temp : keyword;
        temp = temp == null ? null : temp;
        return temp;
    }

    /// <summary>
    /// 获得目标URL的编码类型
    /// </summary>
    /// <returns>返回数据如果没有则返回1</returns>
    public string GetTag()
    {
        string keyword = Request["tag"];
        //url重写类型
        string urlRewriteKeyword = RouteData.Values["tag"] as string;
        string temp = keyword == null && urlRewriteKeyword != null ? urlRewriteKeyword : null;
        temp = string.IsNullOrEmpty(keyword) ? temp : keyword;
        temp = temp == null ? null : temp;
        return temp;
    }
}